package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    //物资来源列表显示
    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize, SupplierVO supplierVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Supplier.class);
        String address = supplierVO.getAddress();    //省市区
        String name = supplierVO.getName();         //具体地址
        String contact = supplierVO.getContact();       //联系人
        Example.Criteria criteria = o.createCriteria();
        if (address != null && !"".equals(address)){
            criteria.andLike("address","%"+address+"%");
        }
        if (name != null && !"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }
        if (contact != null && !"".equals(contact)){
            criteria.andLike("contact","%"+contact+"%");
        }
        List<Supplier> supplierVOList = supplierMapper.selectByExample(o);
        List<SupplierVO> supplierVOS = new ArrayList<>();
        if(!CollectionUtils.isEmpty(supplierVOList)){
            for (Supplier vo : supplierVOList) {
                SupplierVO s = new SupplierVO();
                BeanUtils.copyProperties(vo,s);
                supplierVOS.add(s);
            }
        }
        PageInfo<Supplier> info = new PageInfo<>(supplierVOList);
        return new PageVO<>(info.getTotal(), supplierVOS);
    }

    //添加物资来源
    @Override
    public void add(SupplierVO supplierVO) throws BusinessException{
        @NotBlank(message = "供应商名称不能为空") String address = supplierVO.getAddress();
        String contact = supplierVO.getContact();
        Example o = new Example(Supplier.class);
        o.createCriteria().andEqualTo("contact",contact);
        Supplier sl = new Supplier();
        BeanUtils.copyProperties(supplierVO,sl);
        sl.setModifiedTime(new Date());
        sl.setCreateTime(new Date());
        supplierMapper.insert(sl);
    }

    //删除物资来源
    @Override
    public void delete(Long id) throws BusinessException{
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        if (supplier == null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"物资来源不存在");
        }
        supplierMapper.deleteByPrimaryKey(id);
    }


    //修改之前回显物资来源
    @Override
    public Supplier edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        return supplier;
    }


    //修改物资来源
    @Override
    public void updateSupplier(Long id, Supplier supplierVO) throws BusinessException{
        Integer sort = supplierVO.getSort();
        String contact = supplierVO.getContact();
        String name = supplierVO.getName();
        String phone = supplierVO.getPhone();
        String email = supplierVO.getEmail();
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        if(supplier==null){
            throw new BusinessException(BusinessCodeEnum.USER_ACCOUNT_NOT_FOUND,"要更新的来源不存在");
        }
        Example o = new Example(Supplier.class);
        o.createCriteria().andEqualTo("sort",sort);
        o.createCriteria().andEqualTo("contact",contact);
        o.createCriteria().andEqualTo("name",name);
        o.createCriteria().andEqualTo("phone",phone);
        o.createCriteria().andEqualTo("email",email);
        List<Supplier> suppliers = supplierMapper.selectByExample(o);
        if(!CollectionUtils.isEmpty(suppliers)){
            Supplier supplier1 = suppliers.get(0);
            if(!supplier1.getId().equals(id)){
                throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"该物资来源已经被占用");
            }
        }
        Supplier supplier1 = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier1);
        supplier1.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKeySelective(supplier1);
    }

    @Override
    public List<Supplier> findSupplierList1(String name, String contact, String address) {
        return supplierMapper.selectExt(name, contact, address);

    }
        //添加入库单（物资入库） 物资来源
    /*@Override
    public List<Supplier> findAll(String name, String contact, String contact1) {
        return  supplierMapper.findSupplierList(name,contact,contact1);
    }*/


}
